package com.lz.football.module.compete
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.model.AppData;
	import com.lz.football.module.compete.item.NationTaskItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;

	/**
	 *任务 
	 * @author Administrator
	 * 
	 */	
	public class CompeteTaskView extends BaseView
	{
		/**
		 *捐赠按钮 
		 */
		private var donateBtn_1:SimpleButton;
		/**
		 *捐赠按钮 
		 */
		private var donateBtn_2:SimpleButton;
		/**
		 *捐赠按钮 
		 */
		private var donateBtn_3:SimpleButton;
		
		
		
		/**
		 * 1 为从国家界面打开
		 * 2为任务界面打开  
		 */
		public var type:int;
		private var _itemVec:Vector.<NationTaskItem>;
		
		/**
		 *捐赠类型 
		 */
		private var _donateType:int;
		public function CompeteTaskView()
		{
			super();
		}
		/**
		 * 
		 * @param view
		 * 
		 */
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.compete.taskView");
			this.addChild(symbolSkin);
			symbolSkin.cacheAsBitmap = true;
			_windowBg = symbolSkin.taskBg
			isDrag = true;
			isCloseBtn = true;
			
			_viewHeight = _windowBg.height;
			_viewWidth  = _windowBg.width;
			donateBtn_1 = symbolSkin.donateBtn_1;
			donateBtn_2 = symbolSkin.donateBtn_2;
			donateBtn_3 = symbolSkin.donateBtn_3;
			moveClose(_viewWidth - 30,5);
			onResizeHandler();
			addEvtListener();
			initItem();
			updata();
		}
		/**
		 *初始化ITem 
		 * 
		 */
		private function initItem():void
		{
			_itemVec = new Vector.<NationTaskItem>();
			var item:NationTaskItem;
			for(var i:int = 0;i < 4;i++)
			{
				item = new NationTaskItem();
				item.initUI();
				item.setData(AppData.getInstance().competeDataManager.getTaskByid(i + 401));
				item.x = 20;
				item.y = (item.height )* i + 115;
				this.addChild(item);
				_itemVec.push(item);
			}
		}
		
		
		/**
		 *更新数据 
		 * 
		 */
		public function updata():void
		{
			symbolSkin.treasuryfunds.text = (AppData.getInstance().competeDataManager.competeInfoVo.homecoin * 1000) + "";
		}
		override public function addEvtListener():void
		{
			
			donateBtn_1.addEventListener(MouseEvent.CLICK,ondonateHandler);
			donateBtn_2.addEventListener(MouseEvent.CLICK,ondonateHandler);
			donateBtn_3.addEventListener(MouseEvent.CLICK,ondonateHandler);
			AppEventDispatcher.getInstance().addEventListener(CompeteEvent.TASKISSSUCCESS,onTaskIssSuccessHandler);
			
			DisplayMouseEventManager.instance.addTipsDisplay(donateBtn_1,"捐赠10万欧元，可获得50声望");
			DisplayMouseEventManager.instance.addTipsDisplay(donateBtn_2,"捐赠1000万欧元，可获得5000声望");
			DisplayMouseEventManager.instance.addTipsDisplay(donateBtn_3,"捐赠100点券，可获得100声望");
		}
		
		/**
		 *任务发布成功 
		 * @param event
		 * 
		 */
		protected function onTaskIssSuccessHandler(event:Event):void
		{
			if(type == 1)
			{
				upDateNation();
				updata();
			}else if(type == 2)
			{
//				upDateTaskStatus();
			}
			
		}
		/**
		 *国家界面入口 
		 * 
		 */
		public function upDateNation():void
		{
			var len:int = _itemVec.length;
			for(var i:int = 0;i < len;i++)
			{
				_itemVec[i].updateNationStatus();
			}
			
		}
		/**
		 *更新任务状态  (活动入口)
		 * 
		 */
		public function upDateTaskStatus():void
		{
			var len:int = _itemVec.length;
			for(var i:int = 0;i < len;i++)
			{
				_itemVec[i].upDateTaskStatus();
			}
		}
		
		/**
		 * 捐赠 
		 * @param event
		 * 
		 */		
		protected function ondonateHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			var str:String = "";
			
			switch(event.currentTarget)
			{
				case donateBtn_1:
					if(AppData.getInstance().clubDatamanager.clubInfo.coin <100000)
					{
						Alert.showErrorTip("欧元不足，不可以捐赠！");
						return;
					}
					str = "是否捐赠10万欧元，做为国库资金并获得50声望！";
					_donateType = 1;
					break;
				case donateBtn_2:
					if(AppData.getInstance().clubDatamanager.clubInfo.coin <10000000)
					{
						Alert.showErrorTip("欧元不足，不可以捐赠！");
						return;
					}
					str = "是否捐赠1000万欧元，做为国库资金并获得5000声望！";
					_donateType = 2
					break;
				case donateBtn_3:
					if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint <100)
					{
//						Alert.showErrorTip("点券不足，不可以捐赠！");
						Alert.noCashAlert("现有点券不足100点，是否前往充值？");
						return;
					}
					str = "是否捐赠100点券，做为国库资金并获得100声望！";
					_donateType = 3;
					break;
			}
			
			Alert.show(str,"",Alert.NO,GlobalDef.AlertContainer,sureDonate);
			
		}
		/**
		 *确认捐赠 
		 * @param type
		 * 
		 */		
		private function sureDonate($type:int):void
		{
			if($type == Alert.YES)
			{
				if(type == 1)
				{
					AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.DONATE,{type:_donateType}));
				}else if(type == 2)
				{
					AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.DONATE_FROM_TASK,{type:_donateType}));
				}
			}
		}
		override public function removeEvtListener():void
		{
			donateBtn_1.removeEventListener(MouseEvent.CLICK,ondonateHandler);
			donateBtn_2.removeEventListener(MouseEvent.CLICK,ondonateHandler);
			donateBtn_3.removeEventListener(MouseEvent.CLICK,ondonateHandler);
			
			DisplayMouseEventManager.instance.removeTipsDisplay(donateBtn_1);
			DisplayMouseEventManager.instance.removeTipsDisplay(donateBtn_2);
			DisplayMouseEventManager.instance.removeTipsDisplay(donateBtn_3);
			AppEventDispatcher.getInstance().removeEventListener(CompeteEvent.TASKISSSUCCESS,onTaskIssSuccessHandler);
			super.removeEvtListener();
		}
		override public function destroyView():void
		{
			removeEvtListener();
			while(_itemVec.length)
			{
				_itemVec.pop().destroyView();
			}
			_itemVec = null;
			super.destroyView();
		}
	}
}